﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using VehicleConfigurator.NetUtils.Database;
using System.Windows.Forms;
using System.Configuration;
using System.Globalization;
using VehicleConfigurator.NetUtils.Settings;

namespace VehicleConfigurator.NetUtils
{
    public static class Utils
    {
        public static bool IsNumeric(this Type dataType)
        {
            if (dataType == null)
                //throw new ArgumentNullException("dataType");
                return false;

            return (dataType == typeof(int)
                    || dataType == typeof(double)
                    || dataType == typeof(long)
                    || dataType == typeof(short)
                    || dataType == typeof(float)
                    || dataType == typeof(Int16)
                    || dataType == typeof(Int32)
                    || dataType == typeof(Int64)
                    || dataType == typeof(uint)
                    || dataType == typeof(UInt16)
                    || dataType == typeof(UInt32)
                    || dataType == typeof(UInt64)
                    || dataType == typeof(sbyte)
                    || dataType == typeof(Single)
                   );
        }

        public static bool IsDecimal(this Type dataType)
        {
            if (dataType == null)
                //throw new ArgumentNullException("dataType");
                return false;

            return (dataType == typeof(float)
                    || dataType == typeof(double)
                    || dataType == typeof(decimal)
                   );
        }

        public static bool IsDecimal(this object dataValue)
        {
            if (dataValue == null)
                //throw new ArgumentNullException("dataType");
                return false;

            return (dataValue is decimal
                    || dataValue is double
                    || dataValue is float
                   );
        }


        public static string getIdDelete(List<string> IdDelete)
        {
            string id = "";
            foreach (var i in IdDelete)
            {
                if (id.Equals(""))
                    id += i;
                else
                    id += "," + i;
            }
            return id;
        }

        public static string getIdDelete(DataTable tableDelete, String fileName)
        {
            var ids = string.Empty;
            if (tableDelete != null && tableDelete.Rows.Count > 0)
            {
                foreach (DataRow row in tableDelete.Rows)
                {
                    var optionPriceOid = row[fileName, DataRowVersion.Original]; //"OptionpricelistOid"
                    if (optionPriceOid != null && !string.IsNullOrEmpty(optionPriceOid.ToString()))
                    {
                        ids += "," + optionPriceOid;
                    }
                }
            }

            return ids.Length > 0 ? ids.Substring(1) : string.Empty;
        }

        public static void CopyColumns(DataTable source, DataTable dest, params string[] columns)
        {
            foreach (DataRow sourcerow in source.Rows)
            {
                DataRow destRow = dest.NewRow();
                foreach (string colname in columns)
                {
                    destRow[colname] = sourcerow[colname];
                }
                dest.Rows.Add(destRow);
            }
        }

        public static DataTable CopyColumns(DataTable source, params string[] columns)
        {
            var dest = new DataTable(source.TableName);
            foreach (string colname in columns)
            {
                var coltype = source.Columns[colname].DataType;
                dest.Columns.Add(colname, coltype);
            }

            source.Merge(dest);

            //foreach (DataRow sourcerow in source.Rows)
            //{
            //    DataRow destRow = dest.NewRow();
            //    foreach (string colname in columns)
            //    {
            //        destRow[colname] = sourcerow[colname];
            //    }
            //    dest.Rows.Add(destRow);
            //}
            return dest;
        }
        public static void SetDefault(DataTable dt, ComboBox cb)
        {
            string index = "";
            DataRow[] result = dt.Select("IsDefault = 1");
            if (result.Length > 0)
            {
                DataRow dr = result[0];
                index = dr[FieldNames.OID].ToString();
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                    if (!index.Equals(""))
                        cb.SelectedValue = index;
                    else
                        cb.SelectedIndex = 0;
            }

        }

        public static string GetDefault(this string value, string valDefault)
        {
            return string.IsNullOrEmpty(value) ? valDefault : value;
        }


        public static string toStringFormat(decimal val)
        {

            string temp = "";
            temp = val.ToString("#.####", CultureInfo.InvariantCulture);
            if (string.IsNullOrEmpty(temp))
                temp = "0";
            return temp;


        }
        public static string CommaToDot(string input)
        {
            if (!string.IsNullOrEmpty(input))
            {
                input = input.Replace(",", ".");
            }
            if (string.IsNullOrEmpty(input))
                input = "0";
            return input;
        }

        /// <summary>
        /// Returns a string with backslashes before characters that need to be quoted
        /// </summary>
        /// <param name="InputTxt">Text string need to be escape with slashes</param>
        public static string AddSlashes(this string InputTxt)
        {
            // List of characters handled:
            // \000 null
            // \010 backspace
            // \011 horizontal tab
            // \012 new line
            // \015 carriage return
            // \032 substitute
            // \042 double quote
            // \047 single quote
            // \134 backslash
            // \140 grave accent

            if (InputTxt == null)
            {
                return InputTxt;
            }

            string Result = InputTxt;

            try
            {
                Result = System.Text.RegularExpressions.Regex.Replace(InputTxt, @"[\000\010\011\012\015\032\042\047\134\140]", "\\$0");
            }
            catch (Exception Ex)
            {
                // handle any exception here
                Console.WriteLine(Ex.Message);
            }

            return Result;
        }

        /// <summary>
        /// Un-quotes a quoted string
        /// </summary>
        /// <param name="InputTxt">Text string need to be escape with slashes</param>
        public static string StripSlashes(string InputTxt)
        {
            // List of characters handled:
            // \000 null
            // \010 backspace
            // \011 horizontal tab
            // \012 new line
            // \015 carriage return
            // \032 substitute
            // \042 double quote
            // \047 single quote
            // \134 backslash
            // \140 grave accent

            string Result = InputTxt;

            try
            {
                Result = System.Text.RegularExpressions.Regex.Replace(InputTxt, @"(\\)([\000\010\011\012\015\032\042\047\134\140])", "$2");
            }
            catch (Exception Ex)
            {
                // handle any exception here
                Console.WriteLine(Ex.Message);
            }

            return Result;
        }

        public static decimal GrosCalculate(decimal net, decimal tax)
        {

            decimal gros = 0m;
            gros = net * (1 + tax / 100);
            return gros;

        }
        public static decimal NetCalculate(decimal gros, decimal tax)
        {

            decimal net = 0m;
            net = gros / (1 + tax / 100);
            return net;

        }

    }
}
